import baostock as bs

lg = bs.login()

# 获取历史行情数据
fields = "date,open,high,low,close,volume"
df_bs = bs.query_history_k_data("sh.000001", fields, start_date="2009-01-01",
                end_date="2020-10-28", frequency="d", adjustflag="2")

# frequency="d" 取日k线， adjustflag="3"默认不复权 1::后复权 2:前复权

data_list=[]

while(df_bs.error_code == '0') & df_bs.next():
    # 获取一条记录，将记录合并在一起
    data_list.append(df_bs.get_row_data())

import pandas as pd
result = pd.DataFrame(data_list, columns=df_bs.fields)
result.close = result.close.astype('float64')
result.open = result.open.astype('float64')
result.low = result.low.astype('float64')
result.high = result.high.astype('float64')
result.volume = result.volume.astype('int')

result.index = pd.to_datetime(result.date)

print(result.tail())

print("-----------info----------")
print(result.info())
print()

print("------------axes---------")
print(result.axes)
print()

# 退出系统
bs.logout()